<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/8/6
 * Time: 14:30
 */

namespace app\admin\controller\mall;


use app\common\controller\AdminController;
use think\app;
use think\facade\View;

class OrganTeacher extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = app('app\common\model\business\OrganTeacher');
        $this->storemodel = app('app\common\model\mall\Store');
    }

    public function get_where($array = []){
        $arr = [];
        $id = $this->storemodel->column('id');
        if(!empty($id)){
            $arr[] = ['store_id','in',$id];
        }
        if(isset($array['name']) && !empty($array['name'])){
            $arr[] = ['name','like','%'.$array['name'].'%'];
        }
        return $arr;
    }

    public function get_with(){
        return 'Store';
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $row = $this->model->find($id);


        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            try {
                $post = $this->request->post();
                $rule = [];
                if (method_exists($this, 'get_check_rule')) {
                    $rule = $this->get_check_rule($post ?? []);
                }
                $this->validate($post, $rule);

                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error($e->getMessage());
        }
        $row['teaching'] = explode(',',$row['teaching']);
        View::assign('row', $row);
        if(method_exists($this,'get_other')){
            $this->get_other($this->request->param() ?? []);
        }
        return view('', compact('row'));
    }

    public function action(){

        $param= $this->request->param();

        try{
            $row = $this->model->where('id',$param['id'])->find();
            $row->save($param);

        }catch (\Exception $exception){
            return json(['code'=>1,'msg'=>$exception->getMessage()]);
        }
        return json(['code'=>0,'msg'=>'操作成功']);
    }
}